package com.junzhiit.nengji.api.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.junzhiit.nengji.api.service.FavoriteService;
import com.junzhiit.nengji.api.web.exception.UserNotFoundException;
import com.junzhiit.nengji.api.web.util.APICodeStatus;
import com.junzhiit.nengji.api.web.vo.FavoriteVO;
import com.junzhiit.nengji.api.web.vo.ListVO;
import com.junzhiit.nengji.api.web.vo.ResultVO;
import com.junzhiit.nengji.bo.GoodsBO;
import com.junzhiit.nengji.dao.CartDAO;
import com.junzhiit.nengji.dao.FavoriteDAO;
import com.junzhiit.nengji.dao.GoodsDAO;
import com.junzhiit.nengji.dao.UserDAO;
import com.junzhiit.nengji.model.Favorite;
import com.junzhiit.nengji.model.enums.Is;

@Service
public class FavoriteServiceImpl extends BaseServiceImpl implements FavoriteService {

	@Autowired
	private UserDAO userDAO;

	@Autowired
	private FavoriteDAO favoriteDAO;

	@Autowired
	private GoodsDAO goodsDAO;
	
	@Autowired
	private CartDAO cartDAO;

	public ResultVO add(String goodsId, String userId) {
		ResultVO result = new ResultVO();
		int memberCount = userDAO.checkMember(userId);
		if (memberCount == 0)
			throw new UserNotFoundException("User not found.");

		int goodsCount = goodsDAO.checkGoods(goodsId);
		if (goodsCount == 0) {
			result.setStatus(APICodeStatus.GOODS_NOT_FOUND);
			result.setMessage("Goods not found.");
			return result;
		}

		Favorite favorite = favoriteDAO.getFavorite(userId, goodsId);
		if (favorite != null && StringUtils.isNotBlank(favorite.getId())) {
			result.setData(favorite.getId());
		} else {
			favorite = new Favorite();
			favorite.setGoodsId(goodsId);
			favorite.setUserId(userId);
			favorite = favoriteDAO.insert(favorite);
			result.setData(favorite);
		}
		return result;
	}
	
	@Transactional
	public ResultVO addBatch(String goodsId[], String userId) {
		ResultVO result = new ResultVO();
		int memberCount = userDAO.checkMember(userId);
		if (memberCount == 0) {
			throw new UserNotFoundException("User not found.");
		}
		if (goodsId == null) {
			result.setStatus(APICodeStatus.GOODS_NOT_FOUND);
			result.setMessage("Goods not found.");
			return result;
		}
		List<Favorite> list = new ArrayList<Favorite>();
		for (int i = 0; i < goodsId.length; i++) {
			Favorite favorite = new Favorite();
			favorite.setGoodsId(goodsId[i]);
			favorite.setUserId(userId);
			list.add(favorite);
		}
		//移除购物车内的商品
		cartDAO.batchDeleteGoods(goodsId, userId);
		//删除可能已有的
		favoriteDAO.deleteByGoodsAndUserId(goodsId, userId);
		//添加
		int favoriteCon = favoriteDAO.batchInsert(list);
		result.setData(favoriteCon);
		return result;
	}

	@Transactional
	public ResultVO delete(String userId, String[] favoriteId) {
		ResultVO result = new ResultVO();
		int userCount = userDAO.checkMember(userId);
		if (userCount == 0)
			throw new UserNotFoundException("User not found.");

		/*
		 * int goodsCount = goodsDAO.checkGoods(goodsId); if(goodsCount == 0){
		 * result.setStatus(APICodeStatus.GOODS_NOT_FOUND);
		 * result.setMessage("Goods not found."); return result; }
		 */
		favoriteDAO.batchUpdateToDelete(favoriteId);
		return result;
	}

	public ResultVO getFavorite(Integer pageNumber, Integer maxPageRow, String userId) {
		ResultVO result = new ResultVO();
		int userCount = userDAO.checkMember(userId);
		if (userCount == 0)
			throw new UserNotFoundException("User not found.");

		List<GoodsBO> list = favoriteDAO.getFavoriteList(pageNumber, maxPageRow, userId);
		// if(list.size() == 0){
		// result.setStatus(APICodeStatus.GOODS_NOT_FOUND);
		// result.setMessage("Goods not found.");
		// return result;
		// }

		int totalCount = favoriteDAO.getFavoriteTotalCount(userId);
		List<FavoriteVO> favoriteListVO = new ArrayList<FavoriteVO>();

		for (GoodsBO goodsValue : list) {
			FavoriteVO favoriteVO = new FavoriteVO();
			BeanUtils.copyProperties(goodsValue, favoriteVO);
			favoriteVO.setCoverImage(goodsValue.getCoverImage());
			if (goodsValue.getSkuPrice() != null && goodsValue.getIsOpenSku() == Is.YES.toIntValue()) {
				favoriteVO.setPrice(goodsValue.getSkuPrice());

			} else {
				favoriteVO.setPrice(goodsValue.getPrice());
			}
			favoriteListVO.add(favoriteVO);
		}
		ListVO listVO = new ListVO();
		listVO.setIndex(pageNumber);
		listVO.setCount(totalCount);
		listVO.setList(favoriteListVO);

		result.setData(listVO);
		return result;
	}

}
